package models

import (
	"github.com/astaxie/beego/orm"
	"github.com/satori/go.uuid"
	"time"
)

func init() {

}

type Task struct {
	TaskId       string    `orm:"pk"`
	UserId       string    `json:"userId"`
	Content      string    `json:"content"`
	TaskType     string    `json:"taskType"`
	CreateTime   time.Time `json:"createTime"`
	ExceTaskTime time.Time `json:"exceTaskTime"`
	Status       int16     `json:"status"`
}

//-2  -1  0正常
const (
	del       = -1
	producted = 0
	create    = iota
	exceNow
	exced
)

func CreateTask(userId string, content string, taskType string, exceTaskTime time.Time, status int16) (data interface{}, err error) {
	taskId := uuid.NewV4().String()
	o := orm.NewOrm()
	task := Task{TaskId: taskId,
		UserId:       userId,
		Content:      content,
		TaskType:     taskType,
		CreateTime:   time.Now(),
		ExceTaskTime: exceTaskTime,
		Status:       status}

	_, err = o.Insert(&task)
	if err != nil {
		return "error", err
	}
	return task, err
}

func DelTask(taskId string) (data int64, err error) {
	data, err = UpdateTaskStatus(taskId, del)
	return
}

func UpdateTaskStatus(taskId string, status int16) (data int64, err error) {
	o := orm.NewOrm()
	task := Task{
		TaskId: taskId,
		Status: status}
	data, err = o.Update(&task, "Status")
	return
}
func UpdateTask(taskId string, content string) (data interface{}, err error) {
	o := orm.NewOrm()
	task := Task{TaskId: taskId,
		Content: content}
	data, err = o.Update(&task, "Content")
	return
}

func findTaskById(taskId string) (data interface{}, err error) {
	o := orm.NewOrm()
	task := Task{TaskId: taskId,
		Status: -1}
	data, err = o.Update(&task)
	return
}
